/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comtop.web.report.upload;

import com.comtop.util.report.Converter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author chenquan
 */
@ManagedBean
public class ReportUploadController implements Serializable {

    private String path;
    private String html;
    private UploadedFile file;
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;

    public void upload() throws IOException {
        String appPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
        FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");

        String filePath = appPath + "/" + path;
        File newFilePath = new File(filePath);
        if (!newFilePath.exists()) {
            newFilePath.mkdirs();
        }
        String fullFileName = filePath + "/" + file.getFileName();

        File newFile = new File(fullFileName);
        newFile.setReadOnly();
        FileOutputStream out = new FileOutputStream(newFile);
        out.write(file.getContents());
        out.close();

        System.out.println(newFile.getAbsolutePath());
        new Converter(newFile);
        //sessionBean.getReportFiles().add("/"+path+"/"+file.getFileName().replace(".doc", ".html"));
        html = "/" + path + "/" + file.getFileName().replace(".doc", ".html");
    }

    public String getPath() {
        return path;
    }

    public String getHtml() {
        return html;
    }

    public void setHtml(String html) {
        this.html = html;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }
}
